package com.yidu.best.basicSetup.fundset.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.best.basicSetup.accountset.domain.Account;
import com.yidu.best.basicSetup.accountset.service.AccountService;
import com.yidu.best.basicSetup.custodian.domain.Custodian;
import com.yidu.best.basicSetup.custodian.service.CustodianService;
import com.yidu.best.basicSetup.fundset.domain.Fund;
import com.yidu.best.basicSetup.fundset.domain.FundVo;
import com.yidu.best.basicSetup.fundset.service.FundService;
import com.yidu.best.basicSetup.trusteeset.domain.Trustee;
import com.yidu.best.basicSetup.trusteeset.service.TrusteeService;

@Controller
@RequestMapping("/FundController/")
public class FundController {
	 /**
	  * 业务逻辑层
	  */
	@Autowired
	private FundService fundService;
	@Autowired
	private TrusteeService trusteeService;
	@Autowired
	private CustodianService custodianService;
	@Autowired
	private AccountService accountService;
	
	
	public FundController(FundService fundService) {
		super();
		this.fundService = fundService;
	}

	public FundService getFundService() {
		return fundService;
	}

	public void setFundService(FundService fundService) {
		this.fundService = fundService;
	}

	public FundController() {
		
	}
	
	/**
	 * 跳转页面
	 * @return
	 */
	@RequestMapping(value="fundList", method= RequestMethod.GET)
	public String getFundManager() {
		return "sys/Fund/FundList"; 
	}
	
	/**
	 * 查询所有分页查询
	 * @param fundVo
	 * @return
	 */
	@RequestMapping(value = "queryFund", method= RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> queryFundPage(FundVo fundVo){
		System.out.println("查询所有的方法");
		Map<String,Object> fundMap = fundService.findQueryPageFund(fundVo);
		System.out.println(fundMap);
		return fundMap;
		
	}
	
	
	
	
	/**
	 * 添加
	 * @param fund
	 * @return
	 */
	@RequestMapping(value = "Fund",method = RequestMethod.POST)
	@ResponseBody
	public boolean addFund(Fund fund) {
		System.out.println("abc"+fund);
		int rs = fundService.addFund(fund);
		if(rs==1) {
			return true;
		}
		
		return false;
	}
	
	
	/**
	 * 修改方法
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "updateFund",method = RequestMethod.PUT)
	public boolean updateExchange(Fund fund) {
		System.out.println(fund);
		//调用业务层修改方法
		int rs = fundService.updateFund(fund);
		//如何rs=1 则修改成功
		if(rs==1) {
			//跳到list页面
			return true;
		}else {
			return false;
		}
		
	}
	
	
	/**
	 * 删除
	 * @param area
	 * @return 
	 */
	@ResponseBody
	@RequestMapping(value = "fund/{fundIds}", method = RequestMethod.DELETE)
	public boolean deleteArea(@PathVariable("fundIds")String fundIds ) {
		//定义变量
		int rs=0;
		//定义数组接受前台返回的字符串数组 将,去掉
        String[] fundId = fundIds.split(",");
        //将接收的数组遍历
        for (int i = 0; i < fundId.length; i++) {
        	//调用业务层方法
        	rs = fundService.deleteFund(fundId[i]);
        }
        if(rs==1) {
        	return true;
        }
        return false;
	}
	
	
	
	
	
	
	/**
	 * 查询管理人名的方法
	 * @return   custodianService
	 */
	
	  @RequestMapping(value = "loadCName", method= RequestMethod.GET)
	  @ResponseBody 
	  public List<String> findByAllCustodian(){
		  List<Custodian> custodianList =  custodianService.findByAllCustodian();
	 
		  List<String> str = new ArrayList<>(); 
		  for (Custodian custodian : custodianList) {
			  str.add(custodian.getCustodianName());
			  }
		  		return str; 
		}
	 
	/**
	 * 查询管理人Id的方法
	 * @param cName
	 * @return
	 */
	@RequestMapping(value="getId/{CName}")
	@ResponseBody
	public String getId(@PathVariable("CName")String cName) {
		List<Fund> funds = fundService.findByAllFund();
		String cId = null;
		for(Fund fund : funds) {
			if(fund.getCustodianName().equals(cName)) {
				cId = fund.getCustodianId();
			}
		}
		return cId;
	}
	
	 
	 
	
	/**
	 * 查询托管人名
	 * @return
	 */
	@RequestMapping(value = "trustees", method= RequestMethod.GET)
	@ResponseBody
	public List<String> findByTrusteeFund(){
		List<Trustee> trusteeList = trusteeService.findByAllTrustee();
		
		List<String> str = new ArrayList<>();
		for (Trustee trustee : trusteeList) {
			str.add(trustee.getTrusteeName());
		}	
		return str;                   
	}
	
	/**
	 * 查询托管人Id
	 * @param TName
	 * @return
	 */
	@RequestMapping(value="trusteeId/{TName}")
	@ResponseBody
	public String TrusteeId(@PathVariable("TName")String TName) {
		
		List<Trustee> trustees = trusteeService.findByAllTrustee();
		String tId = null;
		for(Trustee trustee : trustees) {
			if(trustee.getTrusteeName().equals(TName)) {
				tId = trustee.getTrusteeId();
			}
		}
		return tId;
	}
	
	
	
	/**
	 * 查询现金
	 * @return
	 */
	@RequestMapping(value = "account", method= RequestMethod.GET)
	@ResponseBody
	public List<String> findByAllAccount(){
		List<Account> accountList = accountService.findByAllAccount();
		
		List<String> str = new ArrayList<>();
		for (Account account : accountList) {
			str.add(account.getAccountId());
		}	
		return str;                   
	}
}
